package com.example.demo02.dto;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.demo02.constants.CommonConstant;
import com.example.demo02.constants.ParameterConstant;
import com.example.demo02.utils.HelpUtil;
import com.example.demo02.utils.ParamUtils;
import com.sun.javafx.font.PrismFontFactory;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

@Data
public class ShopCouponRespInfo extends ShopCouponQryInfo {
    private String usedSum;
    private String stockSum;
    private String grantExpSum;
    private String usedExpSum;
    private String discountValue;

    public static ShopCouponRespInfo from(ShopCouponQryInfo qryInfo, Map<String,CouponSumSimpleInfo> couponSumSimpleMap) {
        ShopCouponRespInfo respInfo = new ShopCouponRespInfo();
        BeanUtils.copyProperties(qryInfo, respInfo);
        //优惠券类型
        respInfo.setSceneType(ParamUtils.getParamMemo(ParameterConstant.SCENE_STATE, qryInfo.getSceneType()));
        //优惠类型
        respInfo.setDiscountType(ParamUtils.getParamMemo(ParameterConstant.DISCOUNT_TYPE, qryInfo.getDiscountType()));
        //优惠内容
        respInfo.setDiscountValue(assembleDiscountValue(qryInfo));
        //结算类型
        respInfo.setSettlementType(ParamUtils.getParamMemo(ParameterConstant.SETTLEMENT_TYPE,qryInfo.getSettlementType()));
        //定价
        respInfo.setDiscountPrice(HelpUtil.convertToDisplayPrice(qryInfo.getDiscountPrice()));

        CouponSumSimpleInfo info = couponSumSimpleMap.get(respInfo.getOrderids());
        //已使用数量
        respInfo.setUsedSum(info.getUsedSum());
        //使用过期数量
        respInfo.setUsedExpSum(info.getUsedExpSum());
        //发放过期数量
        respInfo.setGrantExpSum(info.getGrantExpSum());
        //库存数量
        respInfo.setStockSum(info.getStockSum());
        return respInfo;
    }

    private static String assembleDiscountValue(ShopCouponQryInfo qryInfo) {
        if (NumberUtil.isNumber(qryInfo.getDiscountType())) {
            switch (Integer.valueOf(qryInfo.getDiscountType())) {
                case 0: {
                    return StrUtil.isNotBlank(qryInfo.getDiscountPrice())
                            ? NumberUtil.toStr(NumberUtil.div(qryInfo.getAmount(), CommonConstant.HUNDRED, 2))
                            : "0.00";
                }
                case 1: {
                    return StrUtil.isNotBlank(qryInfo.getAmount()) ? StrUtil.format("{}分钟", qryInfo.getAmount()) : "";
                }
                case 2: {
                    return "当次免费";
                }
                case 4: {
                    if (NumberUtil.isNumber(qryInfo.getUsePeriodType())) {
                        if (StrUtil.isNotBlank(qryInfo.getUsePeriodStarttime()) && StrUtil.isNotBlank(qryInfo.getUsePeriodEndtime())) {
                            return StrUtil.format("{}-{}", qryInfo.getUsePeriodStarttime(), qryInfo.getUsePeriodEndtime());
                        }
                    }
                    return "";
                }
            }
        }
        return "";
    }

}
